package io.renren.modules.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.car.dao.CarShopMoneyDao;
import io.renren.modules.car.entity.CarBaseEntity;
import io.renren.modules.car.entity.CarFriendShopEntity;
import io.renren.modules.car.entity.CarShopMoneyEntity;
import io.renren.modules.car.service.CarFriendShopService;
import io.renren.modules.car.service.CarShopMoneyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("carShopMoneyService")
public class CarShopMoneyServiceImpl extends ServiceImpl<CarShopMoneyDao, CarShopMoneyEntity> implements CarShopMoneyService {

    @Resource
    private CarShopMoneyDao carShopMoneyDao;

    @Resource
    private CarFriendShopService carFriendShopService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CarShopMoneyEntity> page = this.page(
                new Query<CarShopMoneyEntity>().getPage(params),
                new QueryWrapper<CarShopMoneyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Integer> queryList(Map<String, Object> param) {

        return carShopMoneyDao.queryList(param);
    }

    @Override
    public List<CarShopMoneyEntity> list2(CarBaseEntity carBase) {
        List<CarShopMoneyEntity> carShopMoneyEntities = list(new QueryWrapper<CarShopMoneyEntity>().eq("create_user", carBase.getCreateUser()).eq("car_id", carBase.getId()));

        List<CarFriendShopEntity> list = carFriendShopService.list(new QueryWrapper<CarFriendShopEntity>().eq("create_user", carBase.getCreateUser()).eq("shop_id", carBase.getShopId()));
        carShopMoneyEntities.stream().map(m -> {
            list.stream().filter(c -> Objects.equals(m.getFriendShopId(), c.getId())).anyMatch(c -> {
                m.setFriendShopName(c.getFriendShopName());
                m.setFriendContactPerson(c.getFriendContactPerson());
                return true;
            });
            return true;
        }).collect(Collectors.toList());

        return carShopMoneyEntities;
    }
}
